package com.app.service.impl;

import com.app.mapper.*;
import com.app.model.*;
import com.app.service.AttractInvestmentService;
import com.app.service.KeyWorkService;
import com.app.service.SearchService;
import com.app.service.StriveForFundsService;
import com.app.vo.SearchVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private ExposureMapper exposureMapper;
    @Autowired
    private MatterMapper matterMapper;
    @Autowired
    private ProjectProgressMapper projectProgressMapper;
    @Autowired
    private KeyWorkMapper keyWorkMapper;
    @Autowired
    private DutiesMapper dutiesMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private SystemCategoryMapper systemCategoryMapper;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private PlanBusinessMapper planBusinessMapper;
    @Autowired
    private SupervisionMapper supervisionMapper;

    @Override
    public SearchVo searchAll(String keyword) {
        //根据关键字搜索曝光台
        List<ExposureModel> exposureModelList = exposureMapper.search(keyword);
        int exposureCount = exposureModelList.size();
        //根据关键字搜索书记督
        List<MatterModel> matterModelList = matterMapper.search(keyword);
        int matterCount = matterModelList.size();
        //根据关键字搜索重点投资项目
        List<DepartmentModel> departmentModelList = departmentMapper.search(keyword);
        List<DepartmentModel> department = departmentMapper.getDepartment();
        List<ProjectModel> projectModelList = projectProgressMapper.search(keyword);
        List<ProjectModel> projectList;
        for (int i = 0; i < departmentModelList.size(); i++) {
            projectList = projectProgressMapper.select(departmentModelList.get(i).getId());
            for (int j = 0; j < projectList.size(); j++) {
                for (int i1 = 0; i1 < department.size(); i1++) {
                    if (projectList.get(j).getDepartmentId() == department.get(i1).getId()) {
                        projectList.get(j).setDepartmentText(department.get(i1).getUnitName());
                    }
                }
            }
            if (projectList != null && !projectList.isEmpty()) {
                projectModelList.add(projectList.get(0));
            }
        }
        int projectCount = projectModelList.size();
        //根据关键字搜索争取资金
        List<PlanFundsModel> striveFundsModelList = planMapper.getList();
        List<PlanFundsModel> list = new ArrayList<>();
        int fundsCount = 0;
        if (striveFundsModelList != null) {
            list = striveFundsModelList.stream()
                    .filter(PlanFundsModel -> PlanFundsModel.getDepartmentText().contains(keyword))
                    .collect(Collectors.toList());

            fundsCount = list.size();
        }
        //根据关键字搜索招商引资
        List<PlanBusinessModel> attractInvestmentServiceList = planBusinessMapper.getList();
        List<PlanBusinessModel> businessModelList = new ArrayList<>();
        int businessCount = 0;
        if (attractInvestmentServiceList != null) {
            businessModelList = attractInvestmentServiceList.stream()
                    .filter(PlanBusinessModel -> PlanBusinessModel.getDepartmentText().contains(keyword))
                    .collect(Collectors.toList());
            businessCount = businessModelList.size();
        }
        //根据关键字搜索重点工作任务
        List<KeyWorkModel> keyWorkModelList = keyWorkMapper.search(keyword);
        List<KeyWorkModel> keyWorkList;
        for (int i = 0; i < departmentModelList.size(); i++) {
            keyWorkList = keyWorkMapper.select(departmentModelList.get(i).getId());
            for (int j = 0; j < keyWorkList.size(); j++) {
                for (int i1 = 0; i1 < department.size(); i1++) {
                    if (keyWorkList.get(j).getDepartmentId() == department.get(i1).getId()) {
                        keyWorkList.get(j).setDepartmentText(department.get(i1).getUnitName());
                    }
                }
            }
            if (keyWorkList != null && !keyWorkList.isEmpty()) {
                keyWorkModelList.add(keyWorkList.get(0));
            }
        }
        int keyWorkCount = keyWorkModelList.size();

        int count = exposureCount + matterCount + projectCount + keyWorkCount + fundsCount + businessCount;
        SearchVo vo = new SearchVo();
        vo.setExposureList(exposureModelList);
        vo.setExposureCount(exposureCount);
        vo.setMatterList(matterModelList);
        vo.setMatterCount(matterCount);
        vo.setProjectList(projectModelList);
        vo.setProjectCount(projectCount);
        vo.setFundsList(list);
        vo.setFundsCount(fundsCount);
        vo.setBusinessList(businessModelList);
        vo.setBusinessCount(businessCount);
        vo.setKeyWorkList(keyWorkModelList);
        vo.setKeyWorkCount(keyWorkCount);
        vo.setCount(count);
        return vo;
    }

    @Override
    public ExposureModel selectExposureById(Integer id) {
        ExposureModel exposureModel = exposureMapper.selectById(id);
        if (exposureModel != null) {
            SystemModel type = systemCategoryMapper.selectById(exposureModel.getType());
            exposureModel.setTypeText(type.getName());
        }
        return exposureModel;
    }

    @Override
    public MatterModel selectMatterById(Integer id) {
        MatterModel matterModel = matterMapper.selectById(id);
        if (matterModel != null) {
            DutiesModel dutiesModel = dutiesMapper.selectById(matterModel.getDutiesId());
            matterModel.setDuties(dutiesModel.getDuties());
            matterModel.setName(dutiesModel.getName());
            DutiesModel duty = dutiesMapper.selectById(matterModel.getDutyName());
            if (duty != null) {
                matterModel.setDutyNameText(duty.getName());
            }
            List<ProgressModel> progressModelList = supervisionMapper.selectProgress(id);
            matterModel.setProgress(progressModelList);
        }
        return matterModel;
    }

    @Override
    public ProjectModel selectProjectById(Integer id) {
        ProjectModel projectModel = projectProgressMapper.selectById(id);
        if (projectModel != null) {
            DepartmentModel departmentModel = departmentMapper.selectById(projectModel.getDepartmentId());
            projectModel.setDepartmentText(departmentModel.getUnitName());
            SystemModel c = systemCategoryMapper.selectById(projectModel.getClassId());
            projectModel.setClassText(c.getName());
            SystemModel type = systemCategoryMapper.selectById(projectModel.getType());
            projectModel.setTypeText(type.getName());
            SystemModel status = systemCategoryMapper.selectById(projectModel.getStatus());
            projectModel.setStatusText(status.getName());
        }
        return projectModel;
    }

    @Override
    public KeyWorkModel selectKeyWorkById(Integer id) {
        KeyWorkModel keyWorkModel = keyWorkMapper.selectById(id);
        if (keyWorkModel != null) {
            DepartmentModel departmentModel = departmentMapper.selectById(keyWorkModel.getDepartmentId());
            keyWorkModel.setDepartmentText(departmentModel.getUnitName());
        }
        return keyWorkModel;
    }

    @Override
    public List<KeyWorkModel> selectKeyWorkByDepartmentId(Integer departmentId) {
        QueryWrapper<KeyWorkModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", departmentId);
        return keyWorkMapper.selectList(queryWrapper);
    }
}
